package com.smart.sso.server.util;

import com.smart.sso.server.exception.UtilException;
import com.smart.sso.server.exception.UtilExceptionEnum;
import org.apache.commons.io.IOUtils;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;

@SuppressWarnings("deprecation")
public class HttpUtil {

	private static final String UTF8 = "utf-8";

	/**
	 * 
	 * 向指定 URL 发送GET方法的请求
	 * 
	 * @param url
	 *            发送请求的 URL
	 * 
	 * @param param
	 *            请求参数，请求参数应该是 name1=value1&name2=value2 的形式。
	 * 
	 * @return 所代表远程资源的响应结果
	 * 
	 */
	public static String get(String url, String param) {
		BufferedReader in = null;
		try {
			// 打开和URL之间的连接
			URLConnection connection = new URL(url + "?" + param).openConnection();
			// 设置通用的请求属性
			setURLConnection(connection);
			// 建立实际的连接
			connection.connect();
			in = new BufferedReader(new InputStreamReader(connection.getInputStream(), UTF8));
			StringBuilder sb = new StringBuilder();
			for (String line; (line = in.readLine()) != null;) {
				sb.append(line);
			}
			return sb.toString();
		} catch (Exception e) {
			throw new UtilException(UtilExceptionEnum.HTTP_ERROR, e);
		} finally {
			IOUtils.closeQuietly(in);
		}
	}
	
	
	public static String get(String url) {
		BufferedReader in = null;
		try {
			// 打开和URL之间的连接
			URLConnection connection = new URL(url).openConnection();
			// 设置通用的请求属性
			setURLConnection(connection);
			// 建立实际的连接
			connection.connect();
			in = new BufferedReader(new InputStreamReader(connection.getInputStream(), UTF8));
			StringBuilder sb = new StringBuilder();
			for (String line; (line = in.readLine()) != null;) {
				sb.append(line);
			}
			return sb.toString();
		} catch (Exception e) {
			throw new UtilException(UtilExceptionEnum.HTTP_ERROR, e);
		} finally {
			IOUtils.closeQuietly(in);
		}
	}
	
	/**
	 * 
	 * 向指定 URL 发送POST方法的请求
	 * 
	 * @param url
	 *            发送请求的 URL
	 * 
	 * @param param
	 *            请求参数，请求参数应该是 name1=value1&name2=value2 的形式。
	 * 
	 * @return 所代表远程资源的响应结果
	 * 
	 */
	public static String post(String url, String param) {
		PrintWriter out = null;
		BufferedReader in = null;
		try {
			URLConnection connection = new URL(url).openConnection();
			// 设置通用的请求属性
			setURLConnection(connection);
			// 发送POST请求必须设置如下两行
			connection.setDoOutput(true);
			connection.setDoInput(true);
			// 获取URLConnection对象对应的输出流
			out = new PrintWriter(new OutputStreamWriter(connection.getOutputStream(), UTF8));
			// 发送请求参数
			out.print(param);
			// flush输出流的缓冲
			out.flush();
			// 定义BufferedReader输入流来读取URL的响应
			in = new BufferedReader(new InputStreamReader(connection.getInputStream(), UTF8));
			StringBuilder sb = new StringBuilder();
			for (String line; (line = in.readLine()) != null;) {
				sb.append(line);
			}
			return sb.toString();
		} catch (Exception e) {
			e.printStackTrace();
			throw new UtilException(UtilExceptionEnum.HTTP_ERROR, e);
		} finally {
			IOUtils.closeQuietly(out);
			IOUtils.closeQuietly(in);
		}
	}
	private static void setURLConnection(URLConnection connection) {
		connection.setRequestProperty("accept", "*/*");
		connection.setRequestProperty("connection", "Keep-Alive");
		connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
	}
	
	//edge乱码解决
	public static String encodeFileName(String fileNames ,HttpServletRequest request) {  
		String codedFilename = null;  
		try {  
			String agent = request.getHeader("USER-AGENT");
			if ( null != agent && -1 != agent.indexOf("Edge")) {// ie浏览器及Edge浏览器  
				codedFilename = java.net.URLEncoder.encode(fileNames, "UTF-8");  
			} 
		} catch (Exception e) {  
			e.printStackTrace();  
		}  
		return codedFilename ;  
	}

}